let SWDA_Window_MenuStatus = {};

//-----------------------------------------------------------------------------
// Window_Base
//
// 游戏中所有窗口的超级类。
// 补充Window_Base定义部分内容

// 绘制脸图（根据脸图名，编号）
Window_Base.prototype.SWDA_drawFace = function (actorId, x, y, opacity) {
    const faceName = "StatusFace" + actorId + ((actorId==1 && $gameSwitches.value(299)) ? "a" : "");
    let SWDAStatusFace = new Sprite();
    SWDAStatusFace.bitmap = ImageManager.SWDA_loadSystemFace(faceName);
    SWDAStatusFace.x = x + this.standardPadding() + this.standardPadding();
    SWDAStatusFace.y = y + this.standardPadding() + this.standardPadding();
    SWDAStatusFace.opacity = opacity;
    this._SWDAStatusFace = this._SWDAStatusFace || [];
    this._SWDAStatusFace.push(SWDAStatusFace);
    this.addChildAt(SWDAStatusFace, 3);
};

// 刷新脸图
Window_Base.prototype.SWDA_updateFace = function (index, x, y, opacity) {
    this._SWDAStatusFace[index].x = x + this.standardPadding();
    this._SWDAStatusFace[index].y = y + this.standardPadding();
    this._SWDAStatusFace[index].opacity = opacity;
};

// 绘制HP/MP/TP槽
Window_Base.prototype.SWDA_drawActorSimpleStatus = function (index, x, y, opacity) {
    this.SWDA_drawActorHpVertical(index, x + 3, y + 4, opacity);
    this.SWDA_drawActorMp(index, x + 9, y + 143, opacity);
    this.SWDA_drawActorTpVertical(index, x + 109, y + 4, opacity);
};

// 刷新HP/MP/TP数值
Window_Base.prototype.SWDA_updateActorValue = function (index, x, y) {

    const actor = $gameParty.members()[index];
    var face = "StatusFace" + actor.actorId() + ((actor.actorId()==1 && $gameSwitches.value(299)) ? "a" : "");
    var dyingface = "StatusFaceDying" + actor.actorId() + ((actor.actorId()==1 && $gameSwitches.value(299)) ? "a" : "");
    var deadface = "StatusFaceDead" + actor.actorId() + ((actor.actorId()==1 && $gameSwitches.value(299)) ? "a" : "");
    if (actor.isDying()) {
        this._SWDAStatusFace[index].bitmap = ImageManager.SWDA_loadSystemFace(dyingface);
    } else if (actor.isDead()) {
        this._SWDAStatusFace[index].bitmap = ImageManager.SWDA_loadSystemFace(deadface);
    } else {
        this._SWDAStatusFace[index].bitmap = ImageManager.SWDA_loadSystemFace(face);

    }

    let [rx,ry] = [0,0]
    let [height,width] = [0,0]
    let fillW = 0
    let [color1,color2] = [null,null]

    rx = 3;
    ry = 4;
    height = 136;
    color1 = this.hpGaugeColor1();
    color2 = this.hpGaugeColor2();
    fillW = Math.floor(height * actor.hpRate());
    this._SWDAStatusHPGauge[index].bitmap.clear();
    this._SWDAStatusHPGauge[index].bitmap.fillRect(rx, ry, 7, height, this.gaugeBackColor());
    this._SWDAStatusHPGauge[index].bitmap.gradientFillRect(rx, ry + height - fillW, 7, fillW, color2, color1, true);

    rx = 9;
    ry = 143;
    width = 101;
    color1 = this.mpGaugeColor1();
    color2 = this.mpGaugeColor2();
    fillW = Math.floor(width * actor.mpRate());
    this._SWDAStatusMPGauge[index].bitmap.clear();
    this._SWDAStatusMPGauge[index].bitmap.fillRect(rx, ry, width, 7, this.gaugeBackColor());
    this._SWDAStatusMPGauge[index].bitmap.gradientFillRect(rx, ry, fillW, 7, color1, color2);

    rx = 109;
    ry = 4;
    height = 136;
    color1 = this.tpGaugeColor1();
    color2 = this.tpGaugeColor2();
    fillW = Math.floor(height * actor.tpRate());
    this._SWDAStatusTPGauge[index].bitmap.clear();
    this._SWDAStatusTPGauge[index].bitmap.fillRect(rx, ry, 7, height, this.gaugeBackColor());
    this._SWDAStatusTPGauge[index].bitmap.gradientFillRect(rx, ry + height - fillW, 7, fillW, color2, color1, true);
}

// 刷新HP/MP/TP槽
Window_Base.prototype.SWDA_updateGauges = function (index, x, y, opacity) {
    this._SWDAStatusHPGauge[index].x = x + this.standardPadding();
    this._SWDAStatusHPGauge[index].y = y + this.standardPadding();
    this._SWDAStatusHPGauge[index].opacity = opacity;
    this._SWDAStatusMPGauge[index].x = x + this.standardPadding();
    this._SWDAStatusMPGauge[index].y = y + this.standardPadding();
    this._SWDAStatusMPGauge[index].opacity = opacity;
    this._SWDAStatusTPGauge[index].x = x + this.standardPadding();
    this._SWDAStatusTPGauge[index].y = y + this.standardPadding();
    this._SWDAStatusTPGauge[index].opacity = opacity;
};

// 绘制垂直HP槽
Window_Base.prototype.SWDA_drawActorHpVertical = function (index, x, y, opacity) {
    const actor = $gameParty.members()[index];
    var height = 136;
    var color1 = this.hpGaugeColor1();
    var color2 = this.hpGaugeColor2();
    var fillW = Math.floor(height * actor.hpRate());

    var SWDAStatusHPGauge = SWDAStatusHPGauge || new Sprite();
    SWDAStatusHPGauge.bitmap = new Bitmap(this.SWDFaceWidth(), this.SWDFaceHeight());
    SWDAStatusHPGauge.bitmap.fillRect(x, y, 7, height, this.gaugeBackColor());
    SWDAStatusHPGauge.bitmap.gradientFillRect(x, y + height - fillW, 7, fillW, color2, color1, true);
    SWDAStatusHPGauge.opacity = opacity;
    this._SWDAStatusHPGauge = this._SWDAStatusHPGauge || [];
    this._SWDAStatusHPGauge.push(SWDAStatusHPGauge);
    this.addChildAt(SWDAStatusHPGauge, 0);
};

// 绘制水平MP槽
Window_Base.prototype.SWDA_drawActorMp = function (index, x, y, opacity) {
    const actor = $gameParty.members()[index];
    var width = 101;
    var color1 = this.mpGaugeColor1();
    var color2 = this.mpGaugeColor2();

    var fillW = Math.floor(width * actor.mpRate());
    var SWDAStatusMPGauge = SWDAStatusMPGauge || new Sprite();
    SWDAStatusMPGauge.bitmap = new Bitmap(this.SWDFaceWidth(), this.SWDFaceHeight());
    SWDAStatusMPGauge.bitmap.fillRect(x, y, width, 7, this.gaugeBackColor());
    SWDAStatusMPGauge.bitmap.gradientFillRect(x, y, fillW, 7, color1, color2);
    SWDAStatusMPGauge.opacity = opacity;
    this._SWDAStatusMPGauge = this._SWDAStatusMPGauge || [];
    this._SWDAStatusMPGauge.push(SWDAStatusMPGauge);
    this.addChildAt(SWDAStatusMPGauge, 0);
};

// 绘制垂直TP槽
Window_Base.prototype.SWDA_drawActorTpVertical = function (index, x, y, opacity) {
    const actor = $gameParty.members()[index];
    var height = 136;
    var color1 = this.tpGaugeColor1();
    var color2 = this.tpGaugeColor2();
    var fillW = Math.floor(height * actor.tpRate());

    var SWDAStatusTPGauge = SWDAStatusTPGauge || new Sprite();
    SWDAStatusTPGauge.bitmap = new Bitmap(this.SWDFaceWidth(), this.SWDFaceHeight());
    SWDAStatusTPGauge.bitmap.fillRect(x, y, 7, height, this.gaugeBackColor());
    SWDAStatusTPGauge.bitmap.gradientFillRect(x, y + height - fillW, 7, fillW, color2, color1, true);
    SWDAStatusTPGauge.opacity = opacity;
    this._SWDAStatusTPGauge = this._SWDAStatusTPGauge || [];
    this._SWDAStatusTPGauge.push(SWDAStatusTPGauge);
    this.addChildAt(SWDAStatusTPGauge, 0);
};

// 重写绘制状态图标
//SWDA_Window_MenuStatus.Window_Base_DrawActorIcons = Window_Base.prototype.drawActorIcons;
Window_Base.prototype.drawActorIcons = function (actor, x, y, width, height, opacity) {
    width = width || 144;
    height = height || 0;
    opacity = opacity || 255;
    if (height == 0) {
        if(Imported.CP_StateFloor){
            CP.StateFloorManager.DRAW_ACTOR_ICONS.call(this, actor, x, y, width);
        }else{
            Yanfly.BSC.Window_Base_drawActorIcons.call(this,actor, x, y, width);
            //SWDA_Window_MenuStatus.Window_Base_DrawActorIcons.call(this,actor, x, y, width);
        }
        
    } else {
        var dx = 24;
        var dy = 24;
        var xNum = Math.floor((width - Window_Base._iconWidth) / dx) + 1;
        var yNum = Math.floor((height - Window_Base._iconHeight) / dy) + 1;
        var iconNum = xNum * yNum;

        var icons = actor.allIcons().slice(0, iconNum);

        this.clearIconSprites(actor.actorId());
        for (var i = 0; i < icons.length; i++) {
            var icon_x = x + dx * (i % xNum);
            var icon_y = y + height - Window_Base._iconHeight - dy * Math.floor(i / xNum);
            var state = actor.states()[i];
            this.drawIconSprite(icons[i], icon_x, icon_y, actor, state, opacity);
            /*
            if(CP.StateFloorManager.SHOW_STATE_FLOOR && CP.StateFloorManager.isStateCanAddedFloor(state.id))
                this.drawStateFloor(actor, state, wx, wy);
            */
        }
        
    }
    if ($gameParty.inBattle())
        this.drawActorIconsTurns(actor, x, y, width, height, opacity);
    //this.drawActorStateFloor(actor, x, y, width, height);
};

// 绘制角色状态回合数（覆写）
SWDA_Window_MenuStatus.drawActorIconsTurns = Window_Base.prototype.drawActorIconsTurns;
Window_Base.prototype.drawActorIconsTurns = function(actor, x, y, width, height, opacity) {
    width = width || 144;
    height = height || 0;
    opacity = opacity || 255;

    if (height == 0) {
        SWDA_Window_MenuStatus.drawActorIconsTurns.call(this, actor, x, y, width);
    } else {
        var dx = 24;
        var dy = 24;
        var xNum = Math.floor((width - Window_Base._iconWidth) / dx) + 1;
        var yNum = Math.floor((height - Window_Base._iconHeight) / dy) + 1;
        var iconNum = xNum * yNum;
        var icons = actor.allIcons().slice(0, iconNum);
        for (var i = 0; i < icons.length; ++i) {
            var state = actor.states()[i];
            if (state.iconIndex <= 0) continue;
            var icon_x = x + dx * (i % xNum) - 2;
            var icon_y = y + height - Window_Base._iconHeight - dy * Math.floor(i / xNum) -2;
            if (state.autoRemovalTiming > 0) {
                this.drawStateTurns(actor, state, icon_x, icon_y);
            }
            this.drawStateCounter(actor, state, icon_x, icon_y);
        }

        this.resetFontSettings();
        this.resetTextColor();
    }
};


// 状态层数显示（覆写）
SWDA_Window_MenuStatus.Window_Base_DrawActorStateFloor = Window_Base.prototype.drawActorStateFloor;
if (Imported.CP_StateFloor) {
    Window_Base.prototype.drawActorStateFloor = function (actor, wx, wy, ww, wh) {
        this.resetFontSettings();
        ww = wh || 144;
        wh = wh || 0;
        if (wh == 0) {
            SWDA_Window_MenuStatus.Window_Base_DrawActorStateFloor.call(this, actor, wx, wy, ww);
        } else {
            var dx = 24;
            var dy = 24;
            var xNum = Math.floor((ww - Window_Base._iconWidth) / dx) + 1;
            var yNum = Math.floor((wh - Window_Base._iconHeight) / dy) + 1;
            var iconNum = xNum * yNum;
            var idraw = 0;
            for (var i = 0; i < actor.states().length; i++) {
                var icon_x = wx + dx * (idraw % xNum);
                var icon_y = wy + wh - Window_Base._iconHeight - dy * Math.floor(idraw / xNum);
                var state = actor.states()[i];
                if (state.iconIndex <= 0) continue;
                if(Imported.CP_StateFloor){
                    if (CP.StateFloorManager.SHOW_STATE_FLOOR && CP.StateFloorManager.isStateCanAddedFloor(state.id))
                        this.drawStateFloor(actor, state, icon_x, icon_y);
                    idraw += 1;
                }
            }
        }
    };

}

// 绘制状态sprite
Window_Base.prototype.drawIconSprite = function (iconIndex, x, y, actor, state, opacity) {
    // 画图标
    var actorId = actor.actorId()
    var SWDAStatusIcons = SWDAStatusIcons || new Sprite();
    var bitmap = ImageManager.loadSystem('IconSet');
    var pw = Window_Base._iconWidth;
    var ph = Window_Base._iconHeight;
    var sx = iconIndex % 16 * pw;
    var sy = Math.floor(iconIndex / 16) * ph;

    let wx = 0;
    let wy = 0;

    SWDAStatusIcons.bitmap = bitmap;
    SWDAStatusIcons.setFrame(sx, sy, pw, ph);
    SWDAStatusIcons.x = x + this.padding;
    SWDAStatusIcons.y = y + this.padding;
    SWDAStatusIcons.opacity = opacity;

    // 画状态层数
    if(Imported.CP_StateFloor){
        if (state && CP.StateFloorManager.isStateCanAddedFloor(state.id)) {
            var floor = actor.stateFloor(state.id);
            wx = sx + CP.StateFloorManager.FLOOR_BUFFER_X;
            wy = sy + CP.StateFloorManager.FLOOR_BUFFER_Y;
            var ww = Window_Base._iconWidth;
            var wh = Window_Base.prototype.lineHeight.call(this);

            SWDAStatusIcons.bitmap.fontSize = CP.StateFloorManager.FLOOR_FONT_SIZE;
            SWDAStatusIcons.bitmap.textColor = this.textColor(CP.StateFloorManager.FLOOR_FONT_COLOR);
            SWDAStatusIcons.bitmap.fontFace = this.standardFontFace();
            SWDAStatusIcons.bitmap.outlineWidth = 0;
            SWDAStatusIcons.bitmap.drawText(CP.StateFloorManager.FLOOR_TEXT.format(floor), wx, wy, ww, wh, "left");
        }
    }
    // addChild
    if ($gameParty.inBattle())
        this.addChildAt(SWDAStatusIcons,4);
    else
        this.addChild(SWDAStatusIcons);
    this._SWDAStatusIcons = this._SWDAStatusIcons || [];
    this._SWDAStatusIcons[actorId] = this._SWDAStatusIcons[actorId] || [];
    this._SWDAStatusIcons[actorId].push(SWDAStatusIcons);
};


Window_Base.prototype.clearIconSprites = function (actorId) {
    if (this._SWDAStatusIcons)
        if (this._SWDAStatusIcons[actorId]) {
            for (var i = 0; i < this._SWDAStatusIcons[actorId].length; i++) {
                this.removeChild(this._SWDAStatusIcons[actorId][i]);
            }
        }
}


//-----------------------------------------------------------------------------
// Window_MenuStatus(SWDA)
//
// 显示队伍状态的窗口

// 初始化

Window_MenuStatus.prototype.initialize = function (x, y, mode, ani) {
    var padding = this.standardPadding();
    this._mode = mode || '-';//mode为0时：水平排列。mode为1时，十字形式排列。
    this._ani = ani || false;
    /*
    if (this._mode == '+'){
        var width = (this.SWDFaceWidth()*3 + padding * 2);
        var height = this.SWDFaceHeight()*2 + padding * 2;
    }else{
        var width = (this.SWDFaceWidth() + 60) * 4 + padding * 2;
        var height = this.SWDFaceHeight() + padding * 2;
        var x = (Graphics.boxWidth - width)/2;
        var y = Graphics.boxHeight - height - 64;
    }
    */

    let height = 0;
    let width = this.SWDFaceWidth() * 3 + padding * 2;

    Window_Selectable.prototype.initialize.call(this, x, y, width, height);
    this._pendingIndex = -1;
    this.refresh();
    this.opacity = 100;
    this.cursorOpacity = 100;
    this._list = [];
    this.createList();
    this.createButtons();
};


Window_MenuStatus.prototype.update = function () {
    Window_Base.prototype.update.call(this);
    this.contents.clear();
    this.updateButtons();
    this.updateStatusValue();
    this.updateArrows();
    this.processCursorMove();
    this.processHandling();
    //this.processWheel();
    this.processTouch();
    this._stayCount++;
};

// 更新列表
Window_MenuStatus.prototype.createList = function () {
    for (var i = 0; i < 4; i++) {
        if (i < this.maxItems()) {
            this._list.push({ enabled: true });
        } else {
            this._list.push({ enabled: false });
        }
    }
};

// 获取窗口最大列数
Window_MenuStatus.prototype.maxCols = function () {
    return 4;
};

// 获取最大项目数（队列人数）
Window_MenuStatus.prototype.maxItems = function () {
    return $gameParty.size();
};


// 最大可显示行数
Window_MenuStatus.prototype.numVisibleRows = function () {
    return 1;
    //return 4;
};

// 适配滑条
Window_MenuStatus.prototype.visibleRows = function () {
    return 4;
}

// 定义脸图宽度和高度
Window_Base.prototype.SWDFaceWidth = function () {
    return 120;
}

Window_Base.prototype.SWDFaceHeight = function () {
    return 153;
}

// 读取图片（脸图）
/*
Window_MenuStatus.prototype.loadImages = function() {
    $gameParty.members().forEach(function(actor) {
        ImageManager.reserveFace(actor.faceName());
    }, this);
};
*/
// 创建脸图按钮【初始状态】
Window_MenuStatus.prototype.createButtons = function () {
    var bitmap = ImageManager.loadBitmap('img/system/UI/', 'Button_FaceStatus');
    // 按钮宽高及环绕中心坐标
    this.buttonWidth = this.SWDFaceWidth();
    this.buttonHeight = this.SWDFaceHeight();

    var bW = this.buttonWidth;
    var bH = this.buttonHeight;

    this._buttons = [];

    // 初始化按钮sprite，人物脸图
    for (var i = 0; i < this.maxItems(); i++) {
        this.drawStatusFace(i, 0, 0);

        var button = new Sprite_Button();
        button.bitmap = bitmap;
        button.setColdFrame(0, 0, bW, bH);
        button.setHotFrame(0, bH, bW, bH);
        button.setOnFrame(0, 2 * bH, bW, bH);
        button.visible = true;
        this._buttons.push(button);
        this.addChild(button);
    }

    this.placeButtons();

    if (this._mode == '+') {
        if (this._list[0].enabled) { this._buttons[0].setClickHandler(this.onButtonLeft.bind(this)); }
        if (this._list[1].enabled) { this._buttons[1].setClickHandler(this.onButtonUp.bind(this)); }
        if (this._list[2].enabled) { this._buttons[2].setClickHandler(this.onButtonRight.bind(this)); }
        if (this._list[3].enabled) { this._buttons[3].setClickHandler(this.onButtonDown.bind(this)); }
    } else {
        if (this._list[0].enabled) { this._buttons[0].setClickHandler(this.onButton0.bind(this)); }
        if (this._list[1].enabled) { this._buttons[1].setClickHandler(this.onButton1.bind(this)); }
        if (this._list[2].enabled) { this._buttons[2].setClickHandler(this.onButton2.bind(this)); }
        if (this._list[3].enabled) { this._buttons[3].setClickHandler(this.onButton3.bind(this)); }
    }
};


// 放置按钮
Window_MenuStatus.prototype.placeButtons = function () {
    // 【起始坐标】
    var bW = this.buttonWidth;
    var bH = this.buttonHeight;
    var p = this.standardPadding();
    // 水平排列
    if (this._mode == '-') {
        for (var i = 0; i < this.maxItems(); i++) {
            var X = (this.SWDFaceWidth() + 60) * i;
            var Y = 0;
            this._buttons[i].x = X + p;
            this._buttons[i].y = Y + p + bH / 2;
            this._buttons[i].opacity = 0;
            this.updateStatusFace(i, X, Y, 0);
        }
    }
    // 十字排列
    if (this._mode == '+') {
        var cX = this.SWDFaceWidth() * 1.5 + p;
        var cY = this.SWDFaceHeight() + p;

        if (this._list[0].enabled) {	//左
            X = cX - bW * 0.5;
            Y = cY - bH * 0.5;
            this.updateStatusFace(0, X, Y, 0);
            this._buttons[0].x = X;
            this._buttons[0].y = Y;
            this._buttons[0].opacity = 0;

        }

        if (this._list[1].enabled) {	//上
            X = cX - bW * 0.5;
            Y = cY - bH * 0.5;
            this.updateStatusFace(1, X, Y, 0);
            this._buttons[1].x = X;
            this._buttons[1].y = Y;
            this._buttons[1].opacity = 0;

        }

        if (this._list[2].enabled) {	//右
            X = cX - bW * 0.5;
            Y = cY - bH * 0.5;
            this.updateStatusFace(2, X, Y, 0);
            this._buttons[2].x = X;
            this._buttons[2].y = Y;
            this._buttons[2].opacity = 0;

        }
        if (this._list[3].enabled) {	//下
            X = cX - bW * 0.5;
            Y = cY - bH * 0.5;
            this.updateStatusFace(3, X, Y, 0);
            this._buttons[3].x = X;
            this._buttons[3].y = Y;
            this._buttons[3].opacity = 0;

        }
    }


}

// 刷新脸图按钮（动画）
Window_MenuStatus.prototype.updateButtons = function () {

    var bW = this.buttonWidth;
    var bH = this.buttonHeight;
    var opacity = 0;
    var p = this.standardPadding();
    if (this._ani == true) {
        var speed = 25;
    } else {
        var speed = 0.01;
    }
    var dx = bW / speed;	//13用于调节速度
    var dy = bH / 2 / speed;


    // 【更新不透明度】
    for (var i = 0; i < this.maxItems(); i++) {
        if (this._buttons[i].opacity <= 255) {
            this._buttons[i].opacity += 255 / speed;
            opacity = this._buttons[i].opacity;
        }
    }


    // 【动画坐标】
    // 水平排列
    for (var i = 0; i < this.maxItems(); i++) {
        if (this._mode == '-') {
            var X = (this.SWDFaceWidth() + 60) * i;
            var Y = 0;
            if (this._buttons[i].y > Y + p) {
                Y = this._buttons[i].y - dy;
            } else {
                Y = Y + p;
            }
            X = X + p;
            this.updateStatusFace(i, X - p, Y - p, opacity);
            this._buttons[i].x = X;
            this._buttons[i].y = Y;
        }
    }
    // 十字排列
    if (this._mode == '+') {
        var cX = this.SWDFaceWidth() * 1.5 + p;
        var cY = this.SWDFaceHeight() + p;
        var X = 0;
        var Y = 0;


        if (this._list[0].enabled) {	//左
            if (this._buttons[0].x > cX - bW * 1.5 + 0.01) {
                X = this._buttons[0].x - dx;
            } else {
                X = cX - bW * 1.5;
            }
            Y = this._buttons[0].y;

            this.updateStatusFace(0, X - p, Y - p, opacity);
            this._buttons[0].x = X;
            this._buttons[0].y = Y;
        }


        if (this._list[1].enabled) {	//上
            if (this._buttons[1].y > cY - bH + 0.01) {
                Y = this._buttons[1].y - dy;
            } else {
                Y = cY - bH;
            }
            X = this._buttons[1].x;
            this.updateStatusFace(1, X - p, Y - p, opacity);
            this._buttons[1].x = X;
            this._buttons[1].y = Y;
        }

        if (this._list[2].enabled) {	//右
            if (this._buttons[2].x < cX + bW * 0.5 - 0.01) {
                X = this._buttons[2].x + dx;
            } else {
                X = cX + bW * 0.5;
            }
            Y = this._buttons[2].y;
            this.updateStatusFace(2, X - p, Y - p, opacity);
            this._buttons[2].x = X;
            this._buttons[2].y = Y;

        }
        if (this._list[3].enabled) {	//下
            if (this._buttons[3].y < cY - 0.01) {
                Y = this._buttons[3].y + dy;
            } else {
                Y = cY;
            }
            X = this._buttons[3].x;
            this.updateStatusFace(3, X - p, Y - p, opacity);
            this._buttons[3].x = X;
            this._buttons[3].y = Y;
        }
    }
};


Window_MenuStatus.prototype.drawStatusFace = function (index, x, y) {
    const actor = $gameParty.members()[index];
    this.SWDA_drawActorSimpleStatus(index, x, y, 0);
    this.changePaintOpacity(actor.isBattleMember());
    this.SWDA_drawFace(actor.actorId(), x, y, 0);//宽高需要改成图片默认或置空
    this.drawActorIcons(actor, x + 8, y, this.SWDFaceWidth() - 16, this.SWDFaceHeight() - 12, 0);
    this.changePaintOpacity(true);
};
Window_MenuStatus.prototype.updateStatusFace = function (index, x, y, opacity) {
    const actor = $gameParty.members()[index];
    this.SWDA_updateGauges(index, x, y, opacity);
    this.changePaintOpacity(actor.isBattleMember());
    this.SWDA_updateFace(index, x, y, opacity);
    this.drawActorIcons(actor, x + 8, y, this.SWDFaceWidth() - 16, this.SWDFaceHeight() - 12, opacity);
    this.changePaintOpacity(true);
};

Window_MenuStatus.prototype.updateStatusValue = function () {
    var p = this.standardPadding();
    for (var i = 0; i < this.maxItems(); i++) {
        this.SWDA_updateActorValue(i, this._buttons[i].x - p, this._buttons[i].y - p);
    }
};



/*
Window_MenuStatus.prototype.drawItem = function(index) {
};
*/

// 处理按下确认键的情况
Window_MenuStatus.prototype.processOk = function () {
    Window_Selectable.prototype.processOk.call(this);
    $gameParty.setMenuActor($gameParty.members()[this.index()]);
};


// 选择上一个
Window_MenuStatus.prototype.selectLast = function () {
    if(ConfigManager.commandRemember)
        this.select($gameParty.menuActor().index() || 0);
    else
        this.select(0)
};

// ===================================================================//
// 按钮控制
// ===================================================================//


// 按上下左右键选择4个item
Window_MenuStatus.prototype.processCursorMove = function () {
    var touching = false;
    if (this._cursorAll) {
        for (i = 0; i < 4; i++) {
            if (this._list[i].enabled) {	//修正人数不足4人时报错
                if (this._buttons[i]._touching) {
                    touching = true;
                }
            }
        }
        for (i = 0; i < 4; i++) {

            if (this._list[i].enabled) {
                if (touching) {
                    this._buttons[i].hotFrame();
                } else {
                    this._buttons[i].onFrame();
                }
            }
        }
    }
    if (this.isCursorMovable()) {
        var lastIndex = this.index();

        // 更新图标
        if (TouchInput.movingdate < Input.date) {				//键盘输入的情况
            if (this._mode == '+') {
                if (Input.isRepeated('up') && this._list[1].enabled) {
                    this.cursorUp(Input.isTriggered('up'));
                }
                if (Input.isRepeated('left') && this._list[0].enabled) {
                    this.cursorLeft(Input.isTriggered('left'));
                }
                if (Input.isRepeated('down') && this._list[3].enabled) {
                    this.cursorDown(Input.isTriggered('down'));
                }
                if (Input.isRepeated('right') && this._list[2].enabled) {
                    this.cursorRight(Input.isTriggered('right'));
                }
            } else {
                if (Input.isRepeated('left')) {
                    this.cursorLeft(Input.isTriggered('left'));
                }
                if (Input.isRepeated('right')) {
                    this.cursorRight(Input.isTriggered('right'));
                }
            }

            //如果当前index可用，则高亮，若不可用，则轮换查看哪个可用，切换到第一个可用的。
            if (this._list[this.index()].enabled) {
                this._buttons[this.index()].onFrame();
            } else {
                for (i = 0; (i < 4) && (!this._list[i].enabled); i++) { }
                if (this._list[i].enabled) {
                    this._buttons[i].onFrame();
                    switch (i) {
                        case 0:
                            this.cursorLeft(true);
                            break;
                        case 1:
                            this.cursorUp(true);
                            break;
                        case 2:
                            this.cursorRight(true);
                            break;
                        case 3:
                            this.cursorDown(true);
                            break;
                        default:
                            this.cursorLeft(true);
                            break;
                    }
                }
            }
        } else {	//鼠标/触摸输入的情况
            if (this._mode == '+') {
                if (this._list[0].enabled && this._buttons[0].isMovedInsideFrame()) {
                    this.cursorLeft(true);
                }
                if (this._list[1].enabled && this._buttons[1].isMovedInsideFrame()) {
                    this.cursorUp(true);
                }
                if (this._list[2].enabled && this._buttons[2].isMovedInsideFrame()) {
                    this.cursorRight(true);
                }
                if (this._list[3].enabled && this._buttons[3].isMovedInsideFrame()) {
                    this.cursorDown(true);
                }
            } else {
                if (this._list[0].enabled && this._buttons[0].isMovedInsideFrame()) {
                    this.select(0);
                }
                if (this._list[1].enabled && this._buttons[1].isMovedInsideFrame()) {
                    this.select(1);
                }
                if (this._list[2].enabled && this._buttons[2].isMovedInsideFrame()) {
                    this.select(2);
                }
                if (this._list[3].enabled && this._buttons[3].isMovedInsideFrame()) {
                    this.select(3);
                }
            }
            for (i = 0; i < 4; i++) {
                if (i == this._index) {
                    if (this._list[i].enabled) {
                        this._buttons[i].onFrame();
                    }
                } else {
                    if (this._list[i].enabled) {
                        this._buttons[i].updateFrame();
                    }
                }
            }
        }

        if (this.index() !== lastIndex) {
            SoundManager.playCursor();
        }
        var test = TouchInput._movingdate;
    }
};


Window_MenuStatus.prototype.cursorUp = function (wrap) {
    if (this._mode == '+') {
        this.select(1);
    }
};

Window_MenuStatus.prototype.cursorLeft = function (wrap) {
    if (this._mode == '+') {
        this.select(0);
    } else {
        var index = this.index();
        var maxItems = this.maxItems();
        var maxCols = this.maxCols();
        if (maxCols >= 2 && (index > 0 || (wrap && this.isHorizontal()))) {
            this.select((index - 1 + maxItems) % maxItems);
        }
    }
};

Window_MenuStatus.prototype.cursorDown = function (wrap) {
    if (this._mode == '+') {
        this.select(3);
    }
};

Window_MenuStatus.prototype.cursorRight = function (wrap) {
    if (this._mode == '+') {
        this.select(2);
    } else {
        var index = this.index();
        var maxItems = this.maxItems();
        var maxCols = this.maxCols();
        if (maxCols >= 2 && (index < maxItems - 1 || (wrap && this.isHorizontal()))) {
            this.select((index + 1) % maxItems);
        }
    }
};


Window_MenuStatus.prototype.processTouch = function () {
    if (this.isOpenAndActive()) {
        if (TouchInput.isCancelled()) {
            if (this.isCancelEnabled()) {
                this.processCancel();
            }
        }
    } else {
        this._touching = false;
    }
};



//-----------------------------------------------------------------------------
Window_MenuStatus.prototype.updateButtonsVisiblity = function () {
    if (TouchInput.date > Input.date) {
        this.showButtons();
    } else {
        this.hideButtons();
    }
};

Window_MenuStatus.prototype.showButtons = function () {
    for (var i = 0; i < this._buttons.length; i++) {
        this._buttons[i].visible = true;
    }
};

Window_MenuStatus.prototype.hideButtons = function () {
    for (var i = 0; i < this._buttons.length; i++) {
        this._buttons[i].visible = false;
    }
};


Window_MenuStatus.prototype.onButtonUp = function () {
    SoundManager.playOk();
    this.cursorUp();
    this.processOk();
};

Window_MenuStatus.prototype.onButtonLeft = function () {
    SoundManager.playOk();
    this.cursorLeft();
    this.processOk();
};

Window_MenuStatus.prototype.onButtonDown = function () {
    SoundManager.playOk();
    this.cursorDown();
    this.processOk();
};

Window_MenuStatus.prototype.onButtonRight = function () {
    SoundManager.playOk();
    this.cursorRight();
    this.processOk();
};


Window_MenuStatus.prototype.onButton0 = function () {
    SoundManager.playOk();
    this.select(0);
    //this.cursorLeft();
    this.processOk();
};

Window_MenuStatus.prototype.onButton1 = function () {
    SoundManager.playOk();
    this.select(1);
    //this.cursorUp();
    this.processOk();
};

Window_MenuStatus.prototype.onButton2 = function () {
    SoundManager.playOk();
    this.select(2);
    //this.cursorRight();
    this.processOk();
};

Window_MenuStatus.prototype.onButton3 = function () {
    SoundManager.playOk();
    this.select(3);
    //this.cursorDown();
    this.processOk();
};




// ===================================================================//
// 物品或技能时 使用对象的窗口
// ===================================================================//
//-----------------------------------------------------------------------------
// Window_MenuActor
//
// The window for selecting a target actor on the item and skill screens.

Window_MenuActor.prototype.initialize = function () {
    Window_MenuStatus.prototype.initialize.call(this, 0, 0, '+');
    this.hide();
};
/*
Window_MenuActor.prototype.show = function() {
    this.visible = true;
    this.placeButtons();
};
*/